//
//  ProgressView.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/11.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "ProgressView.h"
#import "ProgressCell.h"
#import "ProgressModel.h"
#import "CBaseTable.h"
#import "ProgressCell.h"
#import "ProgressModel.h"
#define MINIMUM_ZOOM_ARC 0.014 //approximately 1 miles (1 degree of arc ~= 69 miles)
#define ANNOTATION_REGION_PAD_FACTOR 1.5
#define MAX_DEGREES_ARC 360
@interface ProgressView(){
  
    NSMutableDictionary *_dataDic;
    dispatch_source_t _timer;
}
@property (nonatomic, strong) NSString*warningUuid;

@end
@implementation ProgressView
-(instancetype)initWithFrame:(CGRect)frame withWarningUuid:(NSString *)warningUuid
{
    self = [super initWithFrame:frame];
    if (self) {
        _dataDic =[[NSMutableDictionary alloc]init];
//        [self setShowNoDataNotice:@"暂无报修单"];
        [self setShowNoDataNotice:@"没有进行中的报修单"];

        self.backgroundColor =[UIColor clearColor];
        self.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator =NO;
        [self registerCell:[ProgressCell class] withModel:[ProgressModel class]];
        //死的
        [self setDataWithUrl:@"warninglist_orderStateFlow" andParam:@{@"warningUuid":warningUuid}];
        [self setTableActionType:CTableActionType_Both];
        [self loadData];


       
}
    return self;

}
- (void)reloadData
{
    NSInteger click = 0;

    for (int i=0; i<self.mainData.count; i++) {
        ProgressModel *model = [self.mainData objectAtIndex:i];
        NSInteger num =[model.P_state integerValue];
//评价不显示
        if (num==9) {
            [self.mainData removeObjectAtIndex:i];
        }
        //转介第一次显示电话
        if ([model.P_state isEqualToString:@"10000"]) {
            click = 1;
        }
        if (num==3) {
            click++;
            if (click==1) {
                
                model.P_state = @"10000";
                
            }else{
                model.P_state = @"03";
            }
        }

       
    }

        [super reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mainData.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProgressModel *model = [self.mainData objectAtIndex:indexPath.row];
    UIImage *img =[UIImage imageNamed:@"Ico_progress_gray"];
    NSInteger num=[model.P_state integerValue];

   //分配 挂起 转介  完成
    if(num==1||num==5||num==3||num==8)
        
    {
        CGSize labelsize = CGSizeZero;
        
        if ([model.P_content isKindOfClass:[NSNull class]]||[model.P_content isEqualToString:@""]) {
            
        }else
        {
            
            labelsize = [model.P_content sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-img.size.width-64, 900)];
            
        }
        return MAX(labelsize.height + 65-14+10+10, 65);
        
    }else
    {
        return  65;
        
    }


}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
            
        ProgressCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"]; //出列可重用的
        if (cell == nil) {
            cell = [[ProgressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
         cell.backgroundColor =[UIColor clearColor];
    ProgressModel *model1 = [self.mainData objectAtIndex:0];
    [cell setModel:model1];

        if (indexPath.row==0) {
       
            cell.headImg.image=[UIImage imageNamed:@"Ico_progree_green"];
          }else
          {
              cell.headImg.image=[UIImage imageNamed:@"Ico_progress_gray"];

          }
        
//    }
    

//    
    if (indexPath.row==self.mainData.count-1) {
        cell.line.backgroundColor =[UIColor clearColor];

    }
    
    ProgressModel *model = [self.mainData objectAtIndex:indexPath.row];
    [cell setModel:model];
    
   //显示备注的分配、挂起、完成 转介
    NSInteger num=[model.P_state integerValue];

    if(num==1||num==5||num==8||num==3)
    {
        cell.contentLab.text= model.P_content;
    }else
    {
        cell.contentLab.text= @"";
    }
    //报修  转介 显示电话，一个转介不显示电话
    if(num==0||num==10000)
    {
        
        ProgressModel *firstModel = [self.mainData firstObject];
        if ([@[@"完成",@"评价", @"取消"] containsObject:firstModel.P_stateNm]) {
            cell.nameLab.text=[NSString stringWithFormat:@"%@  %@",model.P_name, @""];
            return cell;
        }
        cell.nameLab.text=[NSString stringWithFormat:@"%@  %@",model.P_name, model.P_mobile];
        [cell.nameLab bk_whenTapped:^{
            
            NSString *phone= [CClass replace:model.P_mobile];
            [CClass showAlertTitle:nil hidden:YES context:phone  oneBtn:@"拨打电话" twoBtn:nil handler:^{
                [Common makePhoneWith:model.P_mobile];
            }];
            
        }];

    }else if(num==3)
    {
        cell.nameLab.text=[NSString stringWithFormat:@"%@",model.P_name];
    }else
    {
        cell.nameLab.text=@"";

    }
    if(num==9)
    {
        cell.contentLab.text= @"";
        cell.nameLab.text=@"";
        cell.timeLab.text=@"";
        cell.stateLab.text=@"";
    }
    

    return cell;
    
   
}


@end
